﻿Imports TNCDAL
Imports System.Data.Linq
Imports System.Linq

Public Class CLine
    Dim db As New TanNamChinhDataContext

    Public Sub New()

    End Sub

    Public Sub New(ByVal dataContext As TanNamChinhDataContext)
        db = dataContext
    End Sub

    Public Function SelectAllRecord() As IQueryable(Of tblLine)
        Dim line = From p In db.tblLines
                   Where p.IsActive = True
                  Select p Order By p.LineName

        Return line
    End Function

    Public Function SelectAllRecordByName(ByVal Name As String) As IQueryable(Of tblLine)
        Dim line = From p In db.tblLines
                   Where p.IsActive = True And p.LineName.ToLower.Contains(Name.ToLower)
                  Select p Order By p.LineName

        Return line
    End Function

    Public Function SelectRecordById(ByVal id As Integer) As tblLine
        Dim line = (From p In db.tblLines
                 Where p.ID_Line = id
                 Select p).Single
        Return line
    End Function

#Region "Code HT"
    ''' <summary>
    ''' Lấy danh sách Line theo Name (Name là duy nhất)
    ''' Create date: 23-12-2013
    ''' </summary>
    ''' <param name="name"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectRecordByName(ByVal name As String) As tblLine
        Dim result = (From p In SelectAllRecord()
                 Where p.LineName = name
                 Select p).Single
        Return result
    End Function
#End Region

    Public Sub Insert(ByVal obj As tblLine)
        db.tblLines.InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub

    Public Sub Update(ByVal obj As tblLine)
        Dim line = SelectRecordById(obj.ID_Line)
        line.LineName = obj.LineName
        db.SubmitChanges()
    End Sub

    Public Sub Delete(ByVal id As Integer)
        Dim line = SelectRecordById(id)
        line.IsActive = False
        db.SubmitChanges()
        'Dim line = SelectRecordById(id)
        'db.tblLines.DeleteOnSubmit(line)
        'db.SubmitChanges()
    End Sub
End Class
